package top.tagao.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import top.tagao.yygh.cmn.client.DictFeignClient;
import top.tagao.yygh.hosp.repository.HospitalRepository;
import top.tagao.yygh.hosp.service.HospitalService;

import top.tagao.yygh.model.hosp.BookingRule;
import top.tagao.yygh.model.hosp.Hospital;
import top.tagao.yygh.vo.hosp.HospitalQueryVo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fu-xiao-liu
 * @Date 2022/3/3 15:45
 */
@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paramMap) {
        //先把参数map集合转换为对象Hospital
        String mapString = JSONObject.toJSONString(paramMap);//把map集合变成字符串
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);//把字符串在变成对象

        //先判断是否存在相同的数据
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hoscode);//mongo会自动实现，根据名字

        //如果存在，进行修改
        if(hospitalExist != null){
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setId(hospitalExist.getId());
        }else{//如果不存在，进行添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
        }
        hospital.setIsDeleted(0);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }


    //实现根据医院编号查询
    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital= hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    //医院列表(条件查询分页)
    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //创建pageable对象
        Pageable pageable = PageRequest.of(page-1, limit);

        //创建条件
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        //hospitalSetQueryVo转换Hospital对象
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        Example<Hospital> example = Example.of(hospital,exampleMatcher);

        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

        pages.stream().forEach( item ->{
            this.packHospital(item);
        });

        return pages;
    }

    @Override
    public void updateHospitalStatus(String id, Integer status) {
        if(status == 0 || status == 1) {
            Hospital hospital = hospitalRepository.findById(id).get();
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
        }

    }

    /**
     * @date  根据id查询医院详情
     */
    @Override
    public Map<String, Object> getHospitalById(String id) {
        Hospital hospital = hospitalRepository.findById(id).get();
        this.packHospital(hospital);
        BookingRule bookingRule = hospital.getBookingRule();
        HashMap<String, Object> hashMap = new HashMap<String,Object>();
        hashMap.put("hospital",hospital);
        hashMap.put("bookingRule",bookingRule);
        return hashMap;
    }

    /**
     * @date  通过hoscode获取医院名称
     */
    @Override
    public String getHospitalName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if(hospital != null) {
            return hospital.getHosname();
        }
        return null;
    }

    /**
     * @date   通过医院名称模糊查询医院列表
     */
    @Override
    public List<Hospital> findHospitalListByHosname(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    /**
     * @date  根据医院编号获取医院详情
     */
    @Override
    public Map<String, Object> getHospitalDetail(String hoscode) {
        Map<String, Object> result = new HashMap<>();
        //医院详情
        Hospital hospital = this.getByHoscode(hoscode);
        this.packHospital(hospital);

        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }


    /**
     * 封装数据
     * 获取查询list集合，遍历进行医院等级封装
     */
    private void packHospital(Hospital hospital) {
        //根据dictCode和value获取医院等级名称
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());

        //查询省 市 区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("fullAddress",provinceString+ "-" + cityString + "-" + districtString);
        hospital.getParam().put("hostypeString",hostypeString);
    }




}
